<domain type='<%= @domain_type %>'>
  <name><%= @name %></name>
  <uuid><%= @uuid %></uuid>
  <memory><%= @memory_size %></memory>
  <vcpu><%= @cpus %></vcpu>


  <cpu mode='<%= @cpu_mode %>'>
    <% if @cpu_mode != 'host-passthrough' %>
      <model fallback='<%= @cpu_fallback %>'><%= @cpu_model %></model>
      <% if @nested %>
        <feature policy='optional' name='vmx'/>
        <feature policy='optional' name='svm'/>
      <% end %>
      <% @cpu_features.each do |cpu_feature| %>
        <feature name='<%= cpu_feature[:name] %>' policy='<%= cpu_feature[:policy] %>'/>
      <% end %>
    <% else %>
      <% if @numa_nodes %>
        <numa>
          <% @numa_nodes.each do |node| %>
            <cell id='<%= node[:id] %>' cpus='<%= node[:cpu] %>' memory='<%= node[:mem] %>'/>
          <% end %>
        </numa>
      <% end %>
    <% end %>
  </cpu>


  <os>
    <% if @machine_type %>
      <% if @machine_arch %>
        <type arch='<%= @machine_arch %>' machine='<%= @machine_type %>'>hvm</type>
      <% else %>
        <type machine='<%= @machine_type %>'>hvm</type>
      <% end %>
    <% else %>
      <% if @machine_arch %>
        <type arch='<%= @machine_arch %>'>hvm</type>
      <% else %>
        <type>hvm</type>
      <% end %>
    <% end %>
    <% if @loader %>
      <loader readonly='yes' type='rom'><%= @loader %></loader>
    <% end %>
    <% if @boot_order.count >= 1 %>
      <bootmenu enable='yes'/>
    <% end %>
    <kernel><%= @kernel %></kernel>
    <initrd><%= @initrd %></initrd>
    <cmdline><%= @cmd_line %></cmdline>
    <% if @dtb %>
      <dtb><%= @dtb %></dtb>
    <% end %>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
    <% if @kvm_hidden %>
      <kvm>
        <hidden state='on'/>
      </kvm>
    <% end %>
  </features>
  <clock offset='utc'/>
  <devices>
    <% if @emulator_path %>
    <emulator><%= @emulator_path %></emulator>
    <% end %>
    <% if @domain_volume_path %>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='<%= @domain_volume_cache %>'/>
      <source file='<%= @domain_volume_path %>'/>
      <%# we need to ensure a unique target dev -%>
      <target dev='vda' bus='<%= @disk_bus %>'/>
    </disk>
    <% end %>
<%# additional disks -%>
<% @disks.each do |d| -%>
    <disk type='file' device='disk'>
      <driver name='qemu' type='<%= d[:type] %>' cache='<%= d[:cache] %>'/>
      <source file='<%= d[:absolute_path] %>'/>
      <target dev='<%= d[:device] %>' bus='<%= d[:bus] %>'/>
      <% if d[:shareable] %>
        <shareable/>
      <% end %>
<%# this will get auto generated by libvirt
      <address type='pci' domain='0x0000' bus='0x00' slot='???' function='0x0'/>
-%>
    </disk>
<% end -%>

<% @cdroms.each do |c| %>
    <disk type='file' device='cdrom'>
      <source file='<%= c[:path] %>'/>
      <target dev='<%= c[:dev] %>' bus='<%= c[:bus] %>'/>
      <readonly/>
    </disk>
<% end %>

    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target port='0'/>
    </console>

<% @channels.each do |channel| %>
  <channel type='<%= channel[:type] %>' >
      <source mode='<%= channel[:source_mode] %>'
          <% if channel[:source_path] %>
               path="<%= channel[:source_path] %>"
          <% end %>
      />
      <target type='<%= channel[:target_type] %>'
          <% if channel[:target_name] %>
               name="<%= channel[:target_name] %>"
          <% end %>
          <% if channel[:target_address] %>
               address="<%= channel[:target_address] %>"
          <% end %>
          <% if channel[:target_port] %>
               port="<%= channel[:target_port] %>"
          <% end %>
      />
  </channel>
<% end %>

<% @inputs.each do |input| %>
    <input type='<%= input[:type] %>' bus='<%= input[:bus] %>'/>
<% end %>

    <% if @graphics_type != 'none' %>
      <%# Video device -%>
      <graphics type='<%= @graphics_type %>' port='<%= @graphics_port %>' autoport='<%= @graphics_autoport %>' listen='<%= @graphics_ip %>' keymap='<%= @keymap %>' <%= @graphics_passwd%> />
      <video>
        <model type='<%= @video_type %>' vram='<%= @video_vram %>' heads='1'/>
      </video>
      <%#End Video -%>
    <% end %>
    <% if @rng[:model] == "random"%>
      <rng model='virtio'>
        <backend model='random'>/dev/random</backend>
      </rng>
    <% end %>
    <% @pcis.each do |pci| %>
      <hostdev mode='subsystem' type='pci' managed='yes'>
        <source>
          <address domain='0x0000'
            bus='<%= pci[:bus] %>'
            slot='<%= pci[:slot] %>'
            function='<%= pci[:function] %>'/>
        </source>
      </hostdev>
    <% end %>
    <% @usbs.each do |usb| %>
      <hostdev mode='subsystem' type='usb'>
        <source startupPolicy='<%= usb[:startupPolicy] || "mandatory" %>'>
          <% if usb[:vendor] %>
          <vendor id='<%= usb[:vendor] %>'/>
          <% end %>
          <% if usb[:product] %>
          <product id='<%= usb[:product] %>'/>
          <% end %>
          <% if usb[:bus] && usb[:device] %>
          <address bus='<%= usb[:bus] %>' device='<%= usb[:device] %>'/>
          <% end %>
        </source>
      </hostdev>
    <% end %>

    <% if @tpm_path -%>
    <%# TPM Device -%>
    <tpm model='<%= @tpm_model %>'>
      <backend type='<%= @tpm_type %>'>
        <device path='<%= @tpm_path %>'/>
      </backend>
    </tpm>
    <% end -%>
  </devices>
</domain>
